package org.projectwithoutname.ui.view;


import model.Entity;
import model.impl.ModelFactoryImpl;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.handlers.IHandlerService;
import org.projectwithoutname.commons.Commons;
import org.projectwithoutname.commons.ICommandIds;
import org.projectwithoutname.ui.util.MediaPlayerUtil;
import org.projectwithoutname.ui.util.ScreenToolkit;

public class InformationView extends SuperView {
	
	public static final String ID = ICommandIds.INFORMATION_ID;
	private ScreenToolkit layoutImage, layoutVideo;
	public ScreenToolkit initialLayout;
	private Composite composite;
	private Cursor cursor = null;
	private Entity entity = null;
	private Button imageButton = null;
	private MediaPlayerUtil mediaPlayer;
	
	private void initLayoutImage(){
		layoutImage = new ScreenToolkit(initialLayout, SWT.NONE);
		layoutImage.screenManager(1, 0, false);
		layoutImage.setParent();
		layoutImage.setColor(0, 0, 0);
		initialLayout.putOnScreen(layoutImage, SWT.FILL, SWT.FILL, 0, 0);
		cursor = new Cursor(layoutImage.getDisplay(), SWT.CURSOR_ARROW);
		
		imageButton = new Button(layoutImage, SWT.NONE);
		imageButton.setBackground(new Color(layoutImage.getDisplay(), Commons.COLOR_BLACK));
		layoutImage.putOnScreen(imageButton, SWT.FILL, SWT.FILL, 0, 0);
		
		imageButton.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
					String[] filterExtensions = {"*.JPG","*.JPGE","*.PNG"};
					FileDialog fileDialog = new FileDialog(composite.getShell(), SWT.OPEN);
					fileDialog.setText("Select Image");
					fileDialog.setFilterPath("C:/");
					fileDialog.setFilterExtensions(filterExtensions);
					String selectedFile = fileDialog.open();
					
					if (selectedFile != null){
						Image imageIco = new Image(composite.getDisplay(), new ImageData(selectedFile));
//						layout.setSize(imageIco.getImageData().width, imageIco.getImageData().height);
						imageButton.setImage(imageIco);
						layoutImage.putOnScreen(imageButton, SWT.CENTER, SWT.CENTER, imageIco.getImageData().width, imageIco.getImageData().height);
//						layout.setBackgroundImage(imageIco);
						
						if (entity != null){
							model.Image imagePath = ModelFactoryImpl.eINSTANCE.createImage();
							imagePath.setPath(selectedFile);
							if (entity.getImages().size() > 1)
								entity.getImages().add(1, imagePath);
							else
								entity.getImages().add(imagePath);
							
							IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
							try {
								handlerService.executeCommand(ICommandIds.NOTIFY_INPUT, null);
							} catch (Exception ex) {
								ex.printStackTrace();
								throw new RuntimeException(ICommandIds.NOTIFY_INPUT + " not found");
							}
						}
					}
				}
			});
		
		imageButton.addMouseTrackListener(new MouseTrackListener() {
			@Override
			public void mouseHover(MouseEvent e) {
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				cursor = new Cursor(cursor.getDevice(), SWT.CURSOR_ARROW);
				imageButton.setCursor(cursor);
			}
			
			@Override
			public void mouseEnter(MouseEvent e) {
				cursor = new Cursor(cursor.getDevice(), SWT.CURSOR_HAND);
				imageButton.setCursor(cursor);
			}
		}); 		

	}
	
	private void initLayoutVideo(){
		layoutVideo =  new ScreenToolkit(initialLayout, SWT.NONE);
		layoutVideo.screenManager(1, 0, false);
		layoutVideo.setParent();
		layoutVideo.setColor(0, 0, 0);
		initialLayout.putOnScreen(layoutVideo, SWT.FILL, SWT.FILL, 0, 0);
		
		mediaPlayer = new MediaPlayerUtil(layoutVideo);
		mediaPlayer.open();
		
	}
	
	public void createPartControl(Composite parent) {
		composite = parent;
		initialLayout = new ScreenToolkit(composite, SWT.NONE);
		initialLayout.screenManager(1, 0, false);
		initialLayout.setParent();
		initialLayout.setColor(0,0,0);
		initialLayout.putOnScreen(initialLayout, SWT.FILL, SWT.FILL, composite.getSize().x, composite.getSize().y);
	}
	
	public ScreenToolkit getImageLayout(){
		return layoutImage;
	}
	
	public void setImageLayout(ScreenToolkit layout){
		layoutImage.dispose();
	}
	
	public void setVideoLayout(ScreenToolkit layout){
		layoutVideo = null;
	}
	
	public void showImageLayout(){
		initLayoutImage();
	}
	
	public ScreenToolkit getVideoLayout(){
		return layoutVideo;
	}
	
	public void showVideoLayout(){
		initLayoutVideo();
	}
	
	public void notify(Entity entity){
		this.entity = entity;
		if (getEntity().getImages().size() > 1){
			ImageData data = new ImageData(getEntity().getImages().get(1).getPath());
			Image image = new Image(layoutImage.getDisplay(), data);
			imageButton.setImage(image);
			imageButton.setVisible(true);
			layoutImage.putOnScreen(imageButton, SWT.CENTER, SWT.CENTER, data.width, data.height);
			layoutImage.pack();
		}else{
			imageButton.setBackground(new Color(layoutImage.getDisplay(), Commons.COLOR_BLACK));
			imageButton.setForeground(imageButton.getBackground());
			layoutImage.putOnScreen(imageButton, SWT.FILL, SWT.FILL, initialLayout.getSize().x - 10, initialLayout.getSize().y - 10);
			layoutImage.pack();
		}
	}
	
	public Button getButton(){
		return imageButton;
	}
	
	public void setButton(Button image){
		imageButton = image;
	}
	
	public MediaPlayerUtil getMediaPlayer(){
		return mediaPlayer;
	}
	
	public void setMediaPlayer(MediaPlayerUtil mediaPlayer){
		this.mediaPlayer = mediaPlayer;
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public Entity getEntity() {
		return entity;
	}

}
